import covid.contract_network
from covid.contract_network import society_network
import numpy as np
from  collections import Counter
import matplotlib.pyplot as plt
import seaborn as sns
class paint(society_network):
    def avg_quantities(self):
        sum_p = np.zeros((self.N,))
        matrix = self.touch_record
        for i in range(self.N):
            for j in range(self.N):
                if matrix[i, j] > 0:
                    sum_p[i] += 1
                else:
                    sum_p[i] += 0
        avg_t = np.sum(sum_p) / len(sum_p)
        self.touch_d.append(avg_t)
    def dufenbu(self, matrix):  # 测试当前网络度分布
        '''
        测量当前矩阵度分布
        :param matrix: 输入矩阵
        :return: 分别返回度的概率，度的值和度的频次列表
        '''
        quantities = np.sum(matrix, axis=1)
        pindu = Counter(quantities)
        pindu = dict(sorted(pindu.items()))
        p_num = list(pindu.values())
        p_k = [x / sum(p_num) for x in p_num]
        x_label = list(pindu.keys())
        return p_k,x_label,quantities
    def juleixi(self,matrix):
        '''
        :return: 平均聚类系数
        '''
        G = matrix
        xishu_sum=0
        (m, n) = np.shape(G)
        for i in range(m):
            neighbor_list = []  # i的邻居序号
            c = 0  # i的邻居对为直连的个数
            for j in range(m):
                if G[i, j] == 1:
                    neighbor_list.append(j)
            for neighbor1 in neighbor_list:
                for neighbor2 in neighbor_list:
                    if G[neighbor1, neighbor2] == 1:
                        c = c + 1
            node_sum = len(neighbor_list) * (len(neighbor_list) - 1)
            if node_sum == 0 :
                xishu = 0
            else:
                xishu = c / node_sum  # 聚类系数
            xishu_sum += xishu
            avg_sum = xishu_sum/m
        return avg_sum
def paint_avgquantities(args, args_list, s1, s3, s5, s7):
    plt.plot(range(30), s1.touch_d, color='red', label='{}={}'.format(args, args_list[0]))
    plt.plot(range(30), s3.touch_d, color='green', label='{}={}'.format(args, args_list[1]))
    plt.plot(range(30), s5.touch_d, color='blue', label='{}={}'.format(args, args_list[2]))
    plt.plot(range(30), s7.touch_d, color='pink', label='{}={}'.format(args, args_list[3]))
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.title("m={},np={},p={},nf={}".format(s1.m, s1.Np, s1.p, s1.nf))
    plt.xlabel("天数")
    plt.ylabel("平均每个人的有效接触人数")
    plt.legend()
    plt.show()
def tiaocan(args, args_list):
    N = 100
    p = 0.6
    m = 10
    np = 1
    nf = 1
    if args == 'm':
        society1 = paint(1, 5, N, args_list[0], p, np, nf)  # a,b,N,m,p,np,nf
        society3 = paint(1, 5, N, args_list[1], p, np, nf)
        society5 = paint(1, 5, N, args_list[2], p, np, nf)
        society7 = paint(1, 5, N, args_list[3], p, np, nf)
    elif args == 'p':
        society1 = paint(1, 5, N, m, args_list[0], np, nf)  # a,b,N,m,p,np,nf
        society3 = paint(1, 5, N, m, args_list[1], np, nf)
        society5 = paint(1, 5, N, m, args_list[2], np, nf)
        society7 = paint(1, 5, N, m, args_list[3], np, nf)
    elif args == 'np':
        society1 = paint(1, 5, N, m, p, args_list[0], nf)  # a,b,N,m,p,np,nf
        society3 = paint(1, 5, N, m, p, args_list[1], nf)
        society5 = paint(1, 5, N, m, p, args_list[2], nf)
        society7 = paint(1, 5, N, m, p, args_list[3], nf)
    society1.Network_initialization()
    society3.Network_initialization()
    society5.Network_initialization()
    society7.Network_initialization()
    for day in range(30):
        society1.step(5, 5, 2)
        society1.avg_quantities()
        society3.step(5, 5, 2)
        society3.avg_quantities()
        society5.step(5, 5, 2)
        society5.avg_quantities()
        society7.step(5, 5, 10)
        society7.avg_quantities()
        print(',', end='')

    # pk_1,x1,q1=society1.dufenbu(society1.touch_sum)
    # pk_2,x2,q2=society3.dufenbu(society3.touch_sum)
    # pk_3,x3,q3=society5.dufenbu(society5.touch_sum)
    # pk_4,x4,q4=society7.dufenbu(society7.touch_sum)
    # sns.kdeplot(q1, shade=True, color='r',label='p=0.1')
    # sns.kdeplot(q2, shade=True, color='blue',label='p=0.2')
    # sns.kdeplot(q3, shade=True, color='green',label='p=0.4')
    # sns.kdeplot(q4, shade=True, color='pink',label='p=0.6')
    # plt.rcParams['font.sans-serif'] = ['SimHei']
    # plt.rcParams['axes.unicode_minus'] = False
    # plt.title("交替社会网络度分布")
    # plt.xticks(range(25))
    # plt.yticks([z/10 for z in range(5)])
    # plt.legend()
    # plt.show()
    paint_avgquantities(args, args_list, society1, society3, society5, society7)
tiaocan('p',[0.1,0.2,0.4,0.6])

